package com.example.demo.controller.command;

import com.example.demo.model.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hanqiang
 * @Classname RunCommandContorller
 * @Date 2020/10/5 15:14
 * @Description:
 */
@RestController
@RequestMapping("/command")
public class RunCommandContorller {
    @Autowired
    MongoTemplate mongoTemplate;

    @ApiOperation(value="运行mongoDB命令",notes="这个是自己想要试一试的方法，未测试，不知道是否正确")
    @GetMapping("/runCommand")
    public ResponseData runCommand(){
        String command="{\"buildInfo\":1}";
        return ResponseData.success(mongoTemplate.executeCommand(command));
    }

    @ApiOperation(value="运行mongoDB命令",notes="运行mongoDB命令")
    public ResponseData runDBCommand(){
        String command="{\"buildInfo\":1}";
        Bson bson=Document.parse(command);
        return ResponseData.success(mongoTemplate.getDb().runCommand(bson));
    }
}
